package com.mf.controller;

import io.github.heollhai.common.config.api.ApiScanner;
import io.github.heollhai.common.entity.vo.ApiScannerVo;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.tree.TreeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: lmf
 * @Create: 2024/8/8 17:20
 * Description:
 */
@RestController
@RequestMapping("personnel/apiScanner/v1")
@Tag(name = "api-auth")
@Slf4j
public class ApiScannerController {
    @Resource
    private ApiScanner apiScanner;

    @Operation(summary = "获取指定路径下所有的api")
    @GetMapping("scan")
    public Result<List<ApiScannerVo>> scan() {
        List<Map<String, String>> allApis = apiScanner.findAllApis();

        List<ApiScannerVo> apiScannerVos = new ArrayList<>();
        for (Map<String, String> api : allApis) {
            ApiScannerVo apiScannerVo = new ApiScannerVo();
            apiScannerVo.setPid(api.get("pid"));
            apiScannerVo.setSort(Integer.valueOf(api.get("sort")));
            apiScannerVo.setPath(api.get("path"));
            apiScannerVo.setName(api.get("name"));
            apiScannerVo.setSummary(api.get("summary"));
            apiScannerVo.setId(api.get("id"));
            apiScannerVos.add(apiScannerVo);
        }

        return Result.success(TreeUtils.getRoot(apiScannerVos));
    }
}
